package com.qf.test;

import com.qf.bean.Student;
import com.qf.dao.StudentDao;
import com.qf.util.SessionUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/9
 * Administrator
 * mybatis002
 * 面向对象面向君  不负代码不负卿
 */
public class Demo2 extends SessionUtils {

    //测试
    @Test
    public void c1(){
        try {
            //测试一级缓存
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            SqlSession session = build.openSession();
            StudentDao mapper = session.getMapper(StudentDao.class);
            List<Student> students = mapper.findbynameAndPassword(new HashMap());
            System.out.println("==============================");
            List<Student> students2 = mapper.findbynameAndPassword(new HashMap());
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test  //测试二级缓存
    public void c2(){
        try {
            //测试一级缓存
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            SqlSession session = build.openSession();
            StudentDao mapper = session.getMapper(StudentDao.class);
            List<Student> students = mapper.findbynameAndPassword(new HashMap());
            System.out.println("==============================");
            session.close();
            SqlSession session2 = build.openSession();
            StudentDao mapper2 = session2.getMapper(StudentDao.class);
            List<Student> students2 = mapper2.findbynameAndPassword(new HashMap());
            session2.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test  //测试两表查询
    public void c3(){
        try {
            //测试一级缓存
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            SqlSession session = build.openSession();
            StudentDao mapper = session.getMapper(StudentDao.class);
            List<Student> students = mapper.findallstudent();
            for (Student student : students) {
                System.out.println(student.getSname1()+","+student.getGrade().getCname());
            }
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}


